package com.eascs.web.o2o.web.reward.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.doc.excel.ExcelApiHelper;
import com.eascs.common.exception.RuntimeIOException;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.view.ViewContext;
import com.eascs.tag.MenuTag;
import com.eascs.web.o2o.web.common.BaseMasterViewController;
import com.eascs.web.o2o.web.reward.entity.Province;
import com.eascs.web.o2o.web.reward.entity.ProvinceRewardQueryForm;
import com.eascs.web.o2o.web.reward.service.BaseService;
import com.eascs.web.o2o.web.reward.service.ProvinceRewardService;
import com.eascs.web.o2o.web.util.BankRowWriteHandler;

/**
 * report-10349 省区月度绩效奖金包报表查询, 导入, 导出功能
 * @author hzp
 * @date 2018-03-29
 */
@Controller
@RequestMapping("/provinceReward")
public class ProvinceRewardController extends BaseMasterViewController {
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	ProvinceRewardService prs;

	@Inject
	BaseService baseService;

	//省区月度绩效奖金包报表查询功能
	@RequestMapping(value = "/query")
	public String provinceRewardDetail(ProvinceRewardQueryForm entity) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "provinceRewardDetail");
		String o2ourl = "/provinceRewardRest/query";
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		String date=new SimpleDateFormat("yyyy-MM").format(c.getTime());
		String month = entity.getMonth()==null?date:entity.getMonth();
		entity.setMonth(month);
		String province = entity.getProvince() == null ? "" : entity.getProvince();
		String monthData = entity.getMonth() == null ? "" : entity.getMonth();
		o2ourl = UrlUtil.appendUrl(o2ourl, "month", monthData);
		o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
		o2ourl = o2ourl + "&showPage=";
		List<Province> provinces = baseService.findAllEntity(Province.class);
		ViewContext.put("provinces", provinces);
		ViewContext.put("o2ourl", o2ourl);
		ViewContext.put("month", month);
		ViewContext.put("province", province);
		ViewContext.put("vo", entity);
		return "prov_reward/provinceRewardDetail";
	}

	// 数据导出
	@RequestMapping("/download")
	public void downloadProvinceRewardDetail(ProvinceRewardQueryForm entity, HttpServletResponse httpServletResponse,
			HttpServletRequest request) {
		entity.setPageSize(Integer.MAX_VALUE);
		PageResult<Object[]> pageResult = prs.downProvinceRewardDetail(entity);
		String path = ProvinceRewardController.class.getResource("/excelTemplate").getPath();
		StringBuffer s = new StringBuffer();
		String outPath = s.append(path).append("/provinceReward_detail.xls").toString();
		try {
			String outfile = "省区月度绩效奖金包.xls";
			String userAgent = request.getHeader("User-Agent");
			byte[] bytes     = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			File f = new File(outPath);
			if (!f.exists()) 
				throw new RuntimeIOException("file not found");

			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");

			// 开始的表格，行，列
			int sheetIndex  = 0;
			int rowIndex    = 1;
			int columnIndex = 0;
			// 包含的列
			int col = 11;
			// 需要统计的列
			int[]  arr = {};
			
			BankRowWriteHandler bankRowWriteHandler = new BankRowWriteHandler(pageResult.getData(), col, arr, null, sheetIndex, rowIndex, columnIndex);
			ExcelApiHelper.writeHandle(httpServletResponse.getOutputStream(), RewardCmRdRelationController.class.getResourceAsStream("/excelTemplate/provinceReward_detail.xls"), bankRowWriteHandler);

		} catch (IOException e) {
			throw new RuntimeIOException(e);
		} 
	}
}
